﻿using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using ControleRepresentante.Logic.Models;
using Telerik.Web.Mvc.UI;
using Telerik.Web.Mvc.UI.Fluent;

namespace WebSite.ViewModels.DetailsViewModels.DetailsGridViewModels
{
    public interface IDetailsGridViewModel : IHtmlString
    {
    }

    public abstract class DetailsGridViewModel<TItem> : IDetailsGridViewModel
        where TItem : ModelBase
    {
        protected HtmlHelper HtmlHelper { get; private set; }
        private readonly string m_Name;
        private readonly IEnumerable<TItem> m_Models;

        protected DetailsGridViewModel(HtmlHelper htmlHelper, string name, IEnumerable<TItem> models)
        {
            HtmlHelper = htmlHelper;
            m_Name = name;
            m_Models = models;
        }

        private IHtmlString Render()
        {
            return MvcHtmlString.Create(HtmlHelper.Telerik().Grid(m_Models)
                .Name(m_Name)
                .DataKeys(configurator => configurator.Add(contato => contato.Id))
                .Columns(Columns)
                .Resizable(configurator => configurator.Columns(true))
                .ToHtmlString());
        }

        protected abstract void Columns(GridColumnFactory<TItem> configurator);
        
        public string ToHtmlString()
        {
            return Render().ToHtmlString();
        }
    }
}